package com.lc.YYGH.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.lc.YYGH.cmnclient.cmnclient.DictFeignClient;
import com.lc.YYGH.hosp.repository.HospitalRepository;
import com.lc.YYGH.hosp.service.DepartmentService;
import com.lc.YYGH.hosp.service.HospitalService;
import com.lc.YYGH.model.hosp.BookingRule;
import com.lc.YYGH.model.hosp.Hospital;
import com.lc.YYGH.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

@Service
public class HospitalServiceImpl implements HospitalService{
       @Autowired
       private HospitalRepository hospitalRepository;

       @Autowired
       private DictFeignClient dictFeignClient;



       @Override
       public void save(Map<String, Object> paramMap) {
           Hospital hospital=JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);
           //根据医院编码去查询得到一个Hospital对象
              Hospital targetHospital=hospitalRepository.findByHoscode(hospital.getHoscode());
              if(null==targetHospital)
              {
                     //数据库中不存在
                     hospital.setStatus(0);
                     Date date=new Date();
                     hospital.setCreateTime(date);
                     hospital.setUpdateTime(date);
                     hospitalRepository.save(hospital);
                  System.out.println("test----");
              }
              else
              {
                     targetHospital.setUpdateTime(new Date());
                     targetHospital.setIsDeleted(0);
                     //数据库中已存在，更新数据
                     hospitalRepository.save(targetHospital);

              }

       }

    @Override
    public Hospital findHospital(String hoscode) {
           Hospital hospital=hospitalRepository.findByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        ExampleMatcher macher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example=Example.of(hospital,macher);
        Page<Hospital> pages=hospitalRepository.findAll(example,pageable);
        for(Hospital hosp:pages)
        {
            this.packHospital(hosp);

        }
           return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue()==0||status.intValue()==1)
        {
            Hospital hospital=hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> getDetail(String id) {
           Hospital hospital=hospitalRepository.findById(id).get();
           BookingRule bookingRule=hospital.getBookingRule();
           //创建map存储数据
        Map<String,Object> map=new HashMap<>();
        map.put("hospital",this.packHospital(hospital));
        map.put("bookingRule",bookingRule);

        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hos=hospitalRepository.findByHoscode(hoscode);
        return hos.getHosname();
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> list=hospitalRepository.findByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
       Hospital hospital =hospitalRepository.findByHoscode(hoscode);
        hospital=this.packHospital(hospital);
           Map<String,Object> map=new HashMap();

           map.put("hospital",hospital);
           map.put("bookingRule",hospital.getBookingRule());
           hospital.setBookingRule(null);


        return map;
    }

    private Hospital packHospital(Hospital hospital)
    {
        //调用Dict
       String proname= dictFeignClient.getName(hospital.getProvinceCode());
        String cityname= dictFeignClient.getName(hospital.getCityCode());
        String dicstrictname= dictFeignClient.getName(hospital.getDistrictCode());
        //获取等级名称
  String hostype     = dictFeignClient.getName("Hostype", hospital.getHostype());
    hospital.getParam().put("HostypeName",hostype);
    hospital.getParam().put("fullAddress",proname+cityname+dicstrictname);
    return hospital;
    }
}
